<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/6/7
 * Time: 下午6:49
 */

namespace Modules\Attachment;

use Illuminate\Config\Repository;
use Modules\Attachment\Storage\OssStorage;
use Modules\Attachment\Storage\StorageInterface;

class Upload
{
    /**
     * @var Repository
     */
    protected $config;

    /**
     * @var StorageInterface
     */
    protected $storage;

    public function __construct(Repository $config)
    {
        $this->config = $config;

        $options = $this->config->get('upload');
        if (!$options) {
            $options = require __DIR__ . '/config/upload.php';
        }

        if ($options['driver'] == 'oss') {
            $this->storage = new OssStorage($options['oss']);
        }
    }

    /**
     * 上传文件
     *
     * @param       $key
     * @param       $filePath
     * @param array $options
     *
     * @return mixed
     */
    public function upload($key, $filePath, $options = [])
    {
        return $this->storage->upload($key, $filePath, $options);
    }

    /**
     * 上件内容
     *
     * @param       $key
     * @param       $content
     * @param array $options
     *
     * @return mixed
     */
    public function uploadContent($key, $content, $options = [])
    {
        return $this->storage->uploadContent($key, $content, $options);
    }

    /**
     * 删除文件
     *
     * @param $key
     */
    public function destroy($key)
    {
        $this->storage->destroy($key);
    }
}